package com.maoye.ossp.biz.statement.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.biz.brand.entity.Supplier;
import com.maoye.ossp.biz.brand.service.impl.SupplierServiceImpl;
import com.maoye.ossp.biz.statement.entity.BizStatementPaymentList;
import com.maoye.ossp.biz.statement.entity.BizStatementStmPrepaid;
import com.maoye.ossp.biz.statement.entity.StatementMainConsortium;
import com.maoye.ossp.biz.statement.service.IBizStatementPaymentListService;
import com.maoye.ossp.biz.statement.service.IBizStatementStmCounterService;
import com.maoye.ossp.biz.statement.util.BizStatementConstant;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;

/**
 * 货款支付 Controller
 * 
 * @author yangyf
 * 
 *         2014年3月5日
 */
@Controller
@RequestMapping(value = "statement/stm/paymentList/")
public class BizStatementPaymentListController extends BaseController {
	@Resource
	private IBizStatementPaymentListService paymentListService;

	@Resource
	private IBizStatementStmCounterService stmCounterService;
	
	@Resource
	private SupplierServiceImpl supplierServiceImpl;

	/**
	 * 界面显示 货款支付
	 * 
	 * @param billNo
	 *            参数
	 * @param statementPaymentList
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "list")
	public String list(BizStatementPaymentList statementPaymentList,
			Model model, HttpServletRequest request) {
		// 获取登陆用户Id
		Long userId = UserUtils.getCurrentUserId();
		// 根据当前用户Id 如果是供应商 获取用户获取供应商信息
		Supplier supplier = supplierServiceImpl.get(userId);
		QueryFilter queryFilter = getQuerFilter(request, statementPaymentList);
		// 设置供应商编码
		if (supplier != null) {
			queryFilter.addFilter("supplierNo", supplier.getSupplierNo(),
					QueryOP.EQUAL);
		}
		Page<BizStatementPaymentList> page = (Page<BizStatementPaymentList>) paymentListService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("statementPaymentList", statementPaymentList);
		return resultPage("/biz/statement/paymentlist/list",model,request);
	}

	/**
	 * 查询详细信息
	 * 
	 * @param paymentOrder
	 *            参数
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{paymentOrder}/getOrder")
	public String getOrder(@PathVariable String paymentOrder, Model model) {
		BizStatementPaymentList paymentList = paymentListService
				.getByPaymentOrder(paymentOrder);
		model.addAttribute("paymentList", paymentList);
		return "/biz/statement/paymentlist/view";
	}

	/**
	 * 条件查询
	 * 
	 * @param statementPaymentList
	 *            参数
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "getPaymentCondition")
	public String getPaymentCondition(
			BizStatementPaymentList statementPaymentList, Model model) {

		Map map = new HashMap();
		map.put("isPayment", statementPaymentList.getIsPayment());
		map.put("paymentDate", statementPaymentList.getPaymentDate());
		map.put("supplierNo", statementPaymentList.getSupplierNo());

		List<BizStatementPaymentList> list = paymentListService
				.getByPaymentCondition(map);
		model.addAttribute("list", list);
		return "/biz/statement/paymentlist/list";
	}

	/**
	 * 确认 未支付的
	 * 
	 * @param id
	 *            参数
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{paymentOrder}/updateIsPayment")
	public String updateIsPayment(@PathVariable String paymentOrder,
			RedirectAttributes redirectAttributes, Model model) {
		BizStatementPaymentList paymentList = paymentListService
				.getByPaymentOrder(paymentOrder);
		Map map = new HashMap();
		map.put("id", paymentList.getId());
		map.put("isPayment", BizStatementConstant.IS_PAYMENT);
		paymentListService.confirmedPayment(map);
		List<BizStatementPaymentList> list = paymentListService
				.getPaymentList(map);
		model.addAttribute("list", list);
		addMessage(redirectAttributes, "确认支付成功");
		return "/biz/statement/paymentlist/list";

	}

	/**
	 * 确认收到货款
	 * 
	 * @param paymentOrder
	 *            参数
	 * @param redirectAttributes
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{paymentOrder}/updateIsConfirmed")
	public String updateIsConfirmed(@PathVariable String paymentOrder,
			RedirectAttributes redirectAttributes, Model model) {
		BizStatementPaymentList paymentList = paymentListService
				.getByPaymentOrder(paymentOrder);
		Map map = new HashMap();
		map.put("id", paymentList.getId());
		map.put("isConfirmed", BizStatementConstant.IS_AFFIRM);
		paymentListService.updateIsConfirmed(map);
		List<BizStatementPaymentList> list = paymentListService
				.getPaymentList(map);
		model.addAttribute("list", list);
		addMessage(redirectAttributes, "确认收到货款");
		return "/biz/statement/paymentlist/list";
	}

}
